const Router = require('koa-router')
const goods = new Router()
//！拿到前端的数据
//数据库连接池
const pg = require('pg');
let psql = new pg.Pool({
  database: 'running',
  user: 'run',
  port: 5432,
  host: '82.157.94.115',
  password: 'z12345678',
  max: 2
})

goods.get('/goods', async ctx => {
  // ctx.body = '首页文章推荐'
  let searchContentPostgresql = `select * from goods`;
  let content = await psql.query(searchContentPostgresql);
  // console.log(content)
  // console.log("行数", content.rowCount)

  if (content.rowCount > 0) {
    var goodsObj=[];
    for (var i = 0; i < content.rowCount; i++) {
      var obj = {
        goods_id: content.rows[i].goods_id,
        goods_name: content.rows[i].goods_name,
        goods_title: content.rows[i].goods_title,
        goods_price: content.rows[i].goods_price,
        goods_img: content.rows[i].goods_img
      }
      
      goodsObj.unshift(obj);
    }

    ctx.body = {
        goodsObj
    };
  }


})

module.exports = goods